package com.terrier.ui.effects
{
    import com.greensock.TweenLite;

    import flash.display.DisplayObject;
    import flash.geom.Point;

    public class WxMoveEffect extends WxAbstractEffect
    {
        private var _old:Point;

        private var _from:Point;

        private var _to:Point;

        private var _moveType:String;

        public function WxMoveEffect(from:Point, to:Point, runTime:Number = 0.5,
                                     moveType:String = WxEffectType.LINEAR,
                                     target:DisplayObject = null,
                                     startEventParams:Object = null,
                                     updateEventParams:Object = null,
                                     stopEventParams:Object = null)
        {
            super(target, runTime, startEventParams, updateEventParams, stopEventParams);

            this._from = from;
            this._to = to;
            this._moveType = moveType;
        }

        //--------------------------------------------------------
        //
        // Methods
        //
        //--------------------------------------------------------

        /**
         * @private
         */
        override protected function initialize():void
        {
            if (this.target != null)
            {
                this._old = new Point(this.target.x, this.target.y);
                this.target.x = this._from.x;
                this.target.y = this._from.y;
            }
        }

        override public function start():void
        {
            super.start();
            if (target != null)
            {
                TweenLite.to(target, runTime,
                             { x: this._to.x, y: this._to.y, onComplete: stop });
            }
        }

        /**
         * @private
         */
        override public function restore():void
        {
            if (this.target != null)
            {
                this.target.x = this._old.x;
                this.target.y = this._old.y;
            }
        }

        //--------------------------------------------------------
        //
        // Properties
        //
        //--------------------------------------------------------

        /**
         * Starting scale
         */
        public function get from():Point
        {
            return this._from;
        }

        /**
         * End scale
         */
        public function get to():Point
        {
            return this._to;
        }

        /**
         * Move type. Must be one of the const String values indicated in this class.
         * e.g. MOVE_LINEAR
         */
        public function get moveType():String
        {
            return this._moveType;
        }

        public override function clone():IWxAbstractEffect
        {
            var newEffect:WxMoveEffect = new WxMoveEffect(this._from, this._to, this.runTime,
                                                          this._moveType, this.target, this.startEventParams, this.updateEventParams, this.stopEventParams);

            return newEffect;
        }
    }
}
